package com.cnhis.cloudhealth.clinical.hosdoctor.clinicalpathway.bo;

import com.cnhis.cloudhealth.clinical.hosdoctor.clinicalpathway.dao.ClinicalpathwayDao;
import com.cnhis.cloudhealth.clinical.hosdoctor.doctororder.dao.DoctororderDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by xiezhenyuan on 2016/3/24.
 */
@Service
public class ClinicalpathwayBo {
    @Resource
    private ClinicalpathwayDao clinicalpathwayDao;
    @Autowired
    GetNewId getNewId;

    private List<Map<String,String>> changeToString(List<?> list){
        List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
        for(Object o:list){
            if(o instanceof Map){
                Map<String,Object> pmap = (Map<String,Object>)o;
                Map<String,String> smap = new HashMap<String,String>();
                Iterator<Map.Entry<String, Object>> it = pmap.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<String, Object> entry = it.next();
                    String str = String.valueOf(entry.getValue());
                    smap.put(entry.getKey(),str);
                }
                resultList.add(smap);
            }
        }
        return resultList;
    }

    private ModelVo packModelVo(String nid,Map map){
        ModelVo mo = new ModelVo();
        List<?> list = null;
        try {
            list = clinicalpathwayDao.queryForList(nid,map);
            list = changeToString(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(list!=null&&list.size()>0) {
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            mo.setTotal(list.size());
            mo.setRows(list);
        }
        return mo;
    }
    private ModelVo packModelVoPage(String nid,Map map,int pageno,int pagesize) {
        ModelVo mo = new ModelVo();
        try {
            Page p = clinicalpathwayDao.queryForPage(nid, map, pageno, pagesize);
            if (p.getList() != null && p.getList().size() > 0) {
                mo.setList(p.getList());
                mo.setResult(StaticKeys.OH_SUCCESS);
                mo.setTotal(p.getTotalNumber());
                mo.setRows(p.getList());
                mo.setPage(pageno);
                mo.setRecords(p.getTotalNumber());
                mo.setTotalPage(p.getTotalPage());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mo;
    }


    @SuppressWarnings("unchecked")
    public ModelVo isInpathway (Map map){return packModelVo("Clinicalpathway.7699",map);}
    @SuppressWarnings("unchecked")
    public ModelVo selectNursingprogram(Map map){return packModelVo("Clinicalpathway.7750",map);}
    @SuppressWarnings("unchecked")
    public ModelVo selectPathwayitems(Map map){return packModelVo("Clinicalpathway.7701_001",map);}
    @SuppressWarnings("unchecked")
    public ModelVo selectThePwitem(Map map){return packModelVo("Clinicalpathway.7701_002",map);}
    @SuppressWarnings("unchecked")
    public ModelVo selectPwbeginitem(Map map){return packModelVo("Clinicalpathway.pwbeginitem",map);}
    @SuppressWarnings("unchecked")
    public ModelVo insertClipathway(Map map) throws Exception{
        String lxml = MapUtils.getString(map,"lxml","");
        if(StringUtils.isBlank(lxml)){
            return new ModelVo(StaticKeys.OH_FALSE, "参数为空，请检查参数！");
        }
        //将Json数据装换为list
        List<Map<String,Object>> lxmlList = JsonUtil.getList(lxml);
        map.put("lxmlList",lxmlList);
        Integer lmode = MapUtils.getInteger(map,"lmode",0);
        Integer cn1 = (Integer)clinicalpathwayDao.queryToObject("Clinicalpathway.pathwayinsert_001",map);
        Integer cn2 = (Integer)clinicalpathwayDao.queryToObject("Clinicalpathway.pathwayinsert_002",map);
        if((lmode==0&&cn1>0)||(lmode==1&&cn2>0)){
            return new ModelVo(StaticKeys.OH_FALSE, "此病人已进入路径！");
        }
        List<Map<String,Object>> li3 = (List<Map<String,Object>>)clinicalpathwayDao.queryForList("Clinicalpathway.pathwayinsert_003",map);
        if(li3==null){
            return new ModelVo(StaticKeys.OH_FALSE, "选择的路径其内容信息不全，不能进入此临床路径！");
        }
        Map<String,Object> m1 = (Map<String,Object>)clinicalpathwayDao.queryToObject("Clinicalpathway.pathwayinsert_004",map);
        Integer BCK01C = MapUtils.getInteger(m1,"bck01c");
        Integer BCK01D = MapUtils.getInteger(m1,"bck01d");
        map.put("BCK01C",BCK01C);
        map.put("BCK01D",BCK01D);
        Map<String,Object> m2 = (Map<String,Object>)clinicalpathwayDao.queryToObject("Clinicalpathway.pathwayinsert_005",map);
        Integer bib11 = MapUtils.getInteger(m1,"bib11");
        map.put("bib11",bib11);
        map.put("adate",new Date());
        Integer cn7 = (Integer)clinicalpathwayDao.queryToObject("Clinicalpathway.pathwayinsert_006",map);
        if(cn7>0){
            return new ModelVo(StaticKeys.OH_FALSE, "已经超过当前路径的确诊时间，不能进入此路径！");
        }
        Long VCO01 = getNewId.nextId();
        Long VCN01 = getNewId.nextId();
        map.put("VCO01",VCO01);
        map.put("VCN01",VCN01);
        if(lmode==0){
            clinicalpathwayDao.insert("Clinicalpathway.pathwayinsert_007",map);
        }else if(lmode==1){
            clinicalpathwayDao.insert("Clinicalpathway.pathwayinsert_008",map);
        }
        return new ModelVo(StaticKeys.OH_SUCCESS, "插入成功！");
    }

    @SuppressWarnings("unchecked")
    public ModelVo selectPwinfo(Map map){return packModelVo("Clinicalpathway.7706",map);}
    @SuppressWarnings("unchecked")
    public ModelVo selectPwevalinfo(Map map){return packModelVo("Clinicalpathway.7707",map);}
    @SuppressWarnings("unchecked")
    public ModelVo selectPfeewarnline(Map map){return packModelVo("Clinicalpathway.2042",map);}
    @SuppressWarnings("unchecked")
    public ModelVo warnFeeline(Map map){return packModelVo("Clinicalpathway.4174",map);}
    @SuppressWarnings("unchecked")
    public ModelVo remindRiskvalue(Map map){return packModelVo("Clinicalpathway.7743",map);}
    @SuppressWarnings("unchecked")
    public ModelVo selectPathwayType(){return packModelVo("Clinicalpathway.12063",null);}
    public ModelVo selectPathwayCon(){return packModelVo("Clinicalpathway.14404",null);}
    public ModelVo selectPathwayLim(){return packModelVo("Clinicalpathway.14407",null);}
    public ModelVo selectPathwayXz(){return packModelVo("Clinicalpathway.14411",null);}
    public ModelVo selectPathwayId(Map map)throws Exception{
        Map<String,Object> m1 =(Map<String,Object>)clinicalpathwayDao.queryToObject("Clinicalpathway.7727_001",map);
        Integer BIG07 = MapUtils.getInteger(m1,"big07");
        Integer BIB01 = MapUtils.getInteger(m1,"bib01");
        Integer BIG01 = MapUtils.getInteger(m1,"big01");
        Integer VCN01 = MapUtils.getInteger(m1,"vcn01");
        map.put("BIG07",BIG07);
        map.put("BIB01",BIB01);
        map.put("BIG01",BIG01);
        map.put("VCN01",VCN01);
        return packModelVo("Clinicalpathway.7727_002",map);
    }
    public ModelVo selectMontherchildtype(Map map){return packModelVo("Clinicalpathway.2099",map);}
    public ModelVo selectProimportinfo(Map map,int pageno,int pagesize){return packModelVoPage("Clinicalpathway.7717",map,pageno,pagesize);}
    public ModelVo selectPathwayorder(Map map) throws Exception{
        Integer ltype = MapUtils.getInteger(map,"ltype");
        int productid = 100;
        int programid = 105003;
        int programno = 199;
        map.put("productid",productid);
        map.put("programid",programid);
        map.put("programno",programno);
        Integer ahour = 0;
        String para382 = "";
        if(ltype==1){
            String s1 = (String)clinicalpathwayDao.queryToObject("Clinicalpathway.getsysparam",map);
            ahour = Integer.valueOf(s1);
            map.put("programno",382);
            para382 = (String)clinicalpathwayDao.queryToObject("Clinicalpathway.getsysparam",map);
        }else{
            map.put("programid",105001);
            map.put("programno",199);
            String s1 = (String)clinicalpathwayDao.queryToObject("Clinicalpathway.getsysparam",map);
            ahour = Integer.valueOf(s1);
            map.put("programno",382);
            para382 = (String)clinicalpathwayDao.queryToObject("Clinicalpathway.getsysparam",map);
        }
        Date ndate = org.apache.commons.lang3.time.DateUtils.addHours(new Date(),ahour);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String VAF14 = df.format(ndate);
        map.put("para382",para382);
        map.put("VAF14",VAF14);
        map.put("adate",new Date());
        String BCK03 = (String)clinicalpathwayDao.queryToObject("Clinicalpathway.7718_001",map);
        map.put("BCK03",BCK03);
        return packModelVo("Clinicalpathway.7718_002",map);
    }
    public ModelVo selectPathwayhistory(Map map){return packModelVo("Clinicalpathway.7746",map);}
    public ModelVo selectPathwaytree(Map map){return packModelVo("Clinicalpathway.7716",map);}
    public ModelVo selectMedicalappform(){return packModelVo("Clinicalpathway.6426",null);}
    public ModelVo selectStaffnotice(){return packModelVo("Clinicalpathway.7297",null);}
    public ModelVo selectOrdernature(Map map){return packModelVo("Clinicalpathway.7369",map);}
    public ModelVo selectPchildinfo(Map map){return packModelVo("Clinicalpathway.772",map);}
    public ModelVo selectPtreamlist(Map map){return packModelVo("Clinicalpathway.7314",map);}
    public ModelVo selectPtreamprobyid(Map map){return packModelVo("Clinicalpathway.773",map);}
    /* 临床路径执行Nurse_VCO1_Edit */
    public ModelVo updatePexeline(Map map)throws Exception{
        String lxml = MapUtils.getString(map,"lxml","");
        if(StringUtils.isBlank(lxml)){
            return new ModelVo(StaticKeys.OH_FALSE, "参数为空，请检查参数！");
        }
        //将Json数据装换为list
        List<Map<String,Object>> lxmlList = JsonUtil.getList(lxml);
        if(lxmlList==null||lxmlList.size()==0){
            return new ModelVo(StaticKeys.OH_FALSE, "临床路径执行信息为空！");
        }
        Integer cn = (Integer)clinicalpathwayDao.queryToObject("Clinicalpathway.exeline_001",map);
        if(cn>0){
            return new ModelVo(StaticKeys.OH_FALSE, "此病人已退出临床路径！");
        }
        map.put("lxmlList",lxmlList);
        Integer lmode = MapUtils.getInteger(map,"lmode",0);
        map.put("adate",new Date());
        if(lmode==3){
            clinicalpathwayDao.update("Clinicalpathway.exeline_002",map);
        }else{
            clinicalpathwayDao.update("Clinicalpathway.exeline_003",map);
        }
        return new ModelVo(StaticKeys.OH_SUCCESS, "执行成功！");
    }

    /* 批量路径执行Nurse_VCO1_Batch_Edit */
    public ModelVo updatePexelinebatch(Map map)throws Exception{
        String lxml = MapUtils.getString(map,"lxml","");
        if(StringUtils.isBlank(lxml)){
            return new ModelVo(StaticKeys.OH_FALSE, "参数为空，请检查参数！");
        }
        //将Json数据装换为list
        List<Map<String,Object>> lxmlList = JsonUtil.getList(lxml);
        if(lxmlList==null||lxmlList.size()==0){
            return new ModelVo(StaticKeys.OH_FALSE, "临床路径执行信息为空！");
        }
        Integer cn = (Integer)clinicalpathwayDao.queryToObject("Clinicalpathway.exeline_001",map);
        if(cn>0){
            return new ModelVo(StaticKeys.OH_FALSE, "此病人已退出临床路径！");
        }
        map.put("lxmlList",lxmlList);
        Integer lmode = MapUtils.getInteger(map,"lmode",0);
        map.put("adate",new Date());
        if(lmode==3){
            clinicalpathwayDao.update("Clinicalpathway.exeline_batch_002",map);
        }else{
            clinicalpathwayDao.update("Clinicalpathway.exeline_batch_003",map);
        }
        return new ModelVo(StaticKeys.OH_SUCCESS, "执行成功！");
    }

    /* 临床评估执行Nurse_VCP1_Edit */
    public ModelVo insertevaluation(Map map)throws Exception{
        String lxml = MapUtils.getString(map,"lxml","");
        if(StringUtils.isBlank(lxml)){
            return new ModelVo(StaticKeys.OH_FALSE, "参数为空，请检查参数！");
        }
        //将Json数据装换为list
        List<Map<String,Object>> lxmlList = JsonUtil.getList(lxml);
        if(lxmlList==null||lxmlList.size()==0){
            return new ModelVo(StaticKeys.OH_FALSE, "临床路径评估信息为空！");
        }
        Integer cn = (Integer)clinicalpathwayDao.queryToObject("Clinicalpathway.insertevaluation_001",map);
        if(cn>0){
            return new ModelVo(StaticKeys.OH_FALSE, "此病人已退出临床路径！");
        }
        Integer cn2 = (Integer)clinicalpathwayDao.queryToObject("Clinicalpathway.insertevaluation_002",map);
        if(cn2>0){
            return new ModelVo(StaticKeys.OH_FALSE, "此病人已退出护理计划路径！");
        }

        map.put("lxmlList",lxmlList);
        Integer lmode = MapUtils.getInteger(map,"lmode",0);
        map.put("adate",new Date());

        if(lmode==3) {
            Integer cn3 = (Integer) clinicalpathwayDao.queryToObject("Clinicalpathway.insertevaluation_003", map);
            if (cn3 == 0) {
                Long VCP01 = getNewId.nextId();
                map.put("VCP01",VCP01);
                clinicalpathwayDao.insert("Clinicalpathway.insertevaluation_004", map);
            }else{
                clinicalpathwayDao.update("Clinicalpathway.insertevaluation_005", map);
            }
        }else{
            Integer cn6 = (Integer) clinicalpathwayDao.queryToObject("Clinicalpathway.insertevaluation_006", map);
            if (cn6 == 0) {
                Long VCP01 = getNewId.nextId();
                map.put("VCP01",VCP01);
                clinicalpathwayDao.insert("Clinicalpathway.insertevaluation_007", map);
            }else{
                clinicalpathwayDao.update("Clinicalpathway.insertevaluation_008", map);
            }
        }
        return new ModelVo(StaticKeys.OH_SUCCESS, "执行成功！");
    }

    public ModelVo selectOutreason(){return packModelVo("Clinicalpathway.outreason",null);}
    public ModelVo selectOrdercon(){return packModelVo("Clinicalpathway.ordercon",null);}
    public ModelVo selectDoctorlist(){return packModelVo("Clinicalpathway.doctorlist",null);}
    public ModelVo selectCamdisease(Map map,int pageno,int pagesize){
        return packModelVoPage("Clinicalpathway.1757",map,pageno,pagesize);
    }
    public ModelVo selectBakdisease(Map map,int pageno,int pagesize){
        return packModelVoPage("Clinicalpathway.624",map,pageno,pagesize);
    }
    public ModelVo selectIaddisease(Map map,int pageno,int pagesize){
        return packModelVoPage("Clinicalpathway.625",map,pageno,pagesize);
    }
    public ModelVo selectChangereason(){return packModelVo("Clinicalpathway.7736",null);}


}
